// API基础配置
const API_BASE_URL = 'http://localhost:8080/api'

// 通用请求方法
const request = async (url, options = {}) => {
  const defaultOptions = {
    headers: {
      'Content-Type': 'application/json',
    },
  }
  
  const config = { ...defaultOptions, ...options }
  
  try {
    const response = await fetch(`${API_BASE_URL}${url}`, config)
    const data = await response.json()
    
    if (data.code === 200) {
      return data
    } else {
      throw new Error(data.message || '请求失败')
    }
  } catch (error) {
    console.error('API请求错误:', error)
    throw error
  }
}

// 监控相关API
export const monitorAPI = {
  // 系统概览数据
  getSystemOverview: () => {
    return request('/monitor/overview/system-overview')
  },

  // 系统状态
  getSystemStatus: () => {
    return request('/monitor/overview/system-status')
  },

  // 方法执行排名
  getMethodRanking: (limit = 10) => {
    return request(`/monitor/overview/method-ranking?limit=${limit}`)
  },

  // 性能趋势
  getPerformanceTrend: (startTime, endTime, interval = 'hour') => {
    return request(`/monitor/overview/performance-trend?startTime=${startTime}&endTime=${endTime}&interval=${interval}`)
  },

  // 实时指标
  getRealtimeMetrics: () => {
    return request('/monitor/overview/realtime-metrics')
  },

  // 业务日志
  getBusinessLogs: (params = {}) => {
    return request('/monitor/logs/business', {
      method: 'GET',
      body: JSON.stringify(params)
    })
  },

  // 异常日志
  getExceptionLogs: (params = {}) => {
    return request('/monitor/logs/exceptions', {
      method: 'GET',
      body: JSON.stringify(params)
    })
  },

  // 日志搜索
  searchLogs: (params = {}) => {
    return request('/monitor/logs/search', {
      method: 'POST',
      body: JSON.stringify(params)
    })
  },

  // 获取日志统计
  getLogStats: () => {
    return request('/monitor/logs/stats')
  },

  // 获取实时日志
  getRealtimeLogs: (limit = 20, follow = false) => {
    return request(`/monitor/logs/realtime?limit=${limit}&follow=${follow}`)
  },

  // 慢查询分析
  getSlowQueries: (params = {}) => {
    const queryString = new URLSearchParams(params).toString()
    return request(`/monitor/performance/slow-queries?${queryString}`)
  },

  // 慢查询统计
  getSlowQueryStats: (startTime, endTime) => {
    let url = '/monitor/performance/slow-query-stats'
    if (startTime && endTime) {
      url += `?startTime=${startTime}&endTime=${endTime}`
    }
    return request(url)
  },

  // 慢查询趋势
  getSlowQueryTrend: (sqlType, startTime, endTime, interval = '1h') => {
    let url = `/monitor/performance/slow-query-trend?interval=${interval}`
    if (sqlType) url += `&sqlType=${sqlType}`
    if (startTime) url += `&startTime=${startTime}`
    if (endTime) url += `&endTime=${endTime}`
    return request(url)
  },

  // 性能报告
  getPerformanceReport: (startTime, endTime, reportType = 'daily') => {
    let url = `/monitor/performance/report?reportType=${reportType}`
    if (startTime) url += `&startTime=${startTime}`
    if (endTime) url += `&endTime=${endTime}`
    return request(url)
  },

  // 性能优化建议
  getOptimizationSuggestions: () => {
    return request('/monitor/performance/optimization-suggestions')
  },

  // 数据库性能指标
  getDatabaseMetrics: () => {
    return request('/monitor/performance/database-metrics')
  }
}
